%% This function is to verify the structure of the chorm is valid or not
%     Input: 
%		 		- chorm:        
%				- mpc.branch:	the data after deleting all the information of switches
%		Output:
%				- flag: 0 means not valid, 1 means valid
%   
%   	This function is only available for 119 nodes test system

%		Kaifeng Yang, Feb. 20, 2014
function flag = valid_119(chorm)
further_test = 1;
further_test_2 = 0;
ub = [25;17;15;21;18;12;11;16;12;17;20;18;12;16;20];
for i=1:15
    if chorm(i,1) > ub(i,1) 
        flag = 0;
        further_test = 0;
    end 
    if chorm(i,1) < 1
        flag = 0;
        further_test = 0;
    end
end


if further_test == 1
branch_data =[
1	2	0.0002975	0.0001074	0	100	100	100	0	0	1	-360	360
2	3	0.0002727	0.0000983	0	100	100	100	0	0	1	-360	360
2	4	0.0003719	0.0001339	0	100	100	100	0	0	1	-360	360
4	5	0.0001240	0.0004463	0	100	100	100	0	0	1	-360	360
5	6	0.0001240	0.0004463	0	100	100	100	0	0	1	-360	360
6	7	0.0001240	0.0001033	0	100	100	100	0	0	1	-360	360
7	8	0.0001488	0.0001157	0	100	100	100	0	0	1	-360	360
8	9	0.0001736	0.0005207	0	100	100	100	0	0	1	-360	360
2	10	0.0013719	0.0011107	0	100	100	100	0	0	1	-360	360
10	11	0.0009256	0.0006521	0	100	100	100	0	0	1	-360	360
11	12	0.0015455	0.0025868	0	100	100	100	0	0	1	-360	360
12	13	0.0011736	0.0012496	0	100	100	100	0	0	1	-360	360
13	14	0.0014876	0.0009752	0	100	100	100	0	0	1	-360	360
14	15	0.0012397	0.0003719	0	100	100	100	0	0	1	-360	360
15	16	0.0013223	0.0014876	0	100	100	100	0	0	1	-360	360
16	17	0.0012975	0.0014132	0	100	100	100	0	0	1	-360	360
11	18	0.0018017	0.0023554	0	100	100	100	0	0	1	-360	360
18	19	0.0009752	0.0015289	0	100	100	100	0	0	1	-360	360
19	20	0.0013223	0.0016198	0	100	100	100	0	0	1	-360	360
20	21	0.0009917	0.0015620	0	100	100	100	0	0	1	-360	360
21	22	0.0009917	0.0006521	0	100	100	100	0	0	1	-360	360
22	23	0.0116529	0.0059752	0	100	100	100	0	0	1	-360	360
23	24	0.0024215	0.0011140	0	100	100	100	0	0	1	-360	360
24	25	0.0010992	0.0008595	0	100	100	100	0	0	1	-360	360
25	26	0.0014711	0.0011074	0	100	100	100	0	0	1	-360	360
26	27	0.0014711	0.0011074	0	100	100	100	0	0	1	-360	360
4	28	0.0001240	0.0002446	0	100	100	100	0	0	1	-360	360
28	29	0.0000992	0.0002281	0	100	100	100	0	0	1	-360	360
29	30	0.0009917	0.0022860	0	100	100	100	0	0	1	-360	360
30	31	0.0017355	0.0020083	0	100	100	100	0	0	1	-360	360
31	32	0.0009917	0.0004463	0	100	100	100	0	0	1	-360	360
32	33	0.0014711	0.0019339	0	100	100	100	0	0	1	-360	360
33	34	0.0014711	0.0019339	0	100	100	100	0	0	1	-360	360
34	35	0.0012727	0.0013388	0	100	100	100	0	0	1	-360	360
30	36	0.0015455	0.0021570	0	100	100	100	0	0	1	-360	360
36	37	0.0010992	0.0008182	0	100	100	100	0	0	1	-360	360
29	38	0.0027273	0.0016033	0	100	100	100	0	0	1	-360	360
38	39	0.0025620	0.0016033	0	100	100	100	0	0	1	-360	360
39	40	0.0010744	0.0016033	0	100	100	100	0	0	1	-360	360
40	41	0.0023140	0.0012397	0	100	100	100	0	0	1	-360	360
41	42	0.0097521	0.0070248	0	100	100	100	0	0	1	-360	360
42	43	0.0034711	0.0020132	0	100	100	100	0	0	1	-360	360
43	44	0.0022314	0.0008033	0	100	100	100	0	0	1	-360	360
44	45	0.0028017	0.0010091	0	100	100	100	0	0	1	-360	360
45	46	0.0022314	0.0014702	0	100	100	100	0	0	1	-360	360
35	47	0.0017355	0.0011430	0	100	100	100	0	0	1	-360	360
47	48	0.0009917	0.0006521	0	100	100	100	0	0	1	-360	360
48	49	0.0012397	0.0008157	0	100	100	100	0	0	1	-360	360
49	50	0.0012397	0.0008157	0	100	100	100	0	0	1	-360	360
50	51	0.0019835	0.0013066	0	100	100	100	0	0	1	-360	360
51	52	0.0009917	0.0006521	0	100	100	100	0	0	1	-360	360
52	53	0.0033471	0.0012050	0	100	100	100	0	0	1	-360	360
53	54	0.0033471	0.0012050	0	100	100	100	0	0	1	-360	360
29	55	0.0032314	0.0011653	0	100	100	100	0	0	1	-360	360
55	56	0.0033554	0.0012074	0	100	100	100	0	0	1	-360	360
56	57	0.0033554	0.0012074	0	100	100	100	0	0	1	-360	360
57	58	0.0058347	0.0045132	0	100	100	100	0	0	1	-360	360
58	59	0.0027934	0.0010066	0	100	100	100	0	0	1	-360	360
59	60	0.0027934	0.0010066	0	100	100	100	0	0	1	-360	360
60	61	0.0017107	0.0006174	0	100	100	100	0	0	1	-360	360
61	62	0.0020413	0.0073736	0	100	100	100	0	0	1	-360	360
1	63	0.0002314	0.0003455	0	100	100	100	0	0	1	-360	360
63	64	0.0009669	0.0016661	0	100	100	100	0	0	1	-360	360
64	65	0.0021074	0.0007587	0	100	100	100	0	0	1	-360	360
65	66	0.0017355	0.0006273	0	100	100	100	0	0	1	-360	360
66	67	0.0031653	0.0011405	0	100	100	100	0	0	1	-360	360
67	68	0.0041653	0.0027298	0	100	100	100	0	0	1	-360	360
68	69	0.0033554	0.0012074	0	100	100	100	0	0	1	-360	360
69	70	0.0079504	0.0062893	0	100	100	100	0	0	1	-360	360
70	71	0.0013636	0.0004959	0	100	100	100	0	0	1	-360	360
71	72	0.0025041	0.0009025	0	100	100	100	0	0	1	-360	360
72	73	0.0025041	0.0009025	0	100	100	100	0	0	1	-360	360
73	74	0.0017025	0.0011901	0	100	100	100	0	0	1	-360	360
74	75	0.0019256	0.0006942	0	100	100	100	0	0	1	-360	360
75	76	0.0048843	0.0014653	0	100	100	100	0	0	1	-360	360
76	77	0.0010413	0.0003744	0	100	100	100	0	0	1	-360	360
64	78	0.0046198	0.0030471	0	100	100	100	0	0	1	-360	360
78	79	0.0015372	0.0010140	0	100	100	100	0	0	1	-360	360
79	80	0.0015372	0.0010140	0	100	100	100	0	0	1	-360	360
80	81	0.0021488	0.0011488	0	100	100	100	0	0	1	-360	360
81	82	0.0012727	0.0012231	0	100	100	100	0	0	1	-360	360
82	83	0.0019008	0.0010579	0	100	100	100	0	0	1	-360	360
83	84	0.0020826	0.0008760	0	100	100	100	0	0	1	-360	360
84	85	0.0014876	0.0012231	0	100	100	100	0	0	1	-360	360
79	86	0.0013223	0.0015041	0	100	100	100	0	0	1	-360	360
86	87	0.0016529	0.0019008	0	100	100	100	0	0	1	-360	360
87	88	0.0013223	0.0032479	0	100	100	100	0	0	1	-360	360
65	89	0.0055289	0.0019934	0	100	100	100	0	0	1	-360	360
89	90	0.0021983	0.0010140	0	100	100	100	0	0	1	-360	360
90	91	0.0021983	0.0010140	0	100	100	100	0	0	1	-360	360
91	92	0.0021983	0.0010140	0	100	100	100	0	0	1	-360	360
92	93	0.0021983	0.0010140	0	100	100	100	0	0	1	-360	360
93	94	0.0019256	0.0009504	0	100	100	100	0	0	1	-360	360
94	95	0.0040992	0.0011405	0	100	100	100	0	0	1	-360	360
91	96	0.0016198	0.0014876	0	100	100	100	0	0	1	-360	360
96	97	0.0016198	0.0014876	0	100	100	100	0	0	1	-360	360
97	98	0.0015421	0.0010083	0	100	100	100	0	0	1	-360	360
98	99	0.0006165	0.0026281	0	100	100	100	0	0	1	-360	360
1	100	0.0005165	0.0002190	0	100	100	100	0	0	1	-360	360
100	101	0.0012405	0.0019339	0	100	100	100	0	0	1	-360	360
101	102	0.0011132	0.0007339	0	100	100	100	0	0	1	-360	360
102	103	0.0019066	0.0009942	0	100	100	100	0	0	1	-360	360
103	104	0.0036942	0.0013289	0	100	100	100	0	0	1	-360	360
104	105	0.0013488	0.0004860	0	100	100	100	0	0	1	-360	360
105	106	0.0027273	0.0008182	0	100	100	100	0	0	1	-360	360
106	107	0.0012893	0.0004636	0	100	100	100	0	0	1	-360	360
107	108	0.0031562	0.0011355	0	100	100	100	0	0	1	-360	360
108	109	0.0013438	0.0004835	0	100	100	100	0	0	1	-360	360
109	110	0.0031562	0.0011355	0	100	100	100	0	0	1	-360	360
110	111	0.0020207	0.0007264	0	100	100	100	0	0	1	-360	360
110	112	0.0017256	0.0006223	0	100	100	100	0	0	1	-360	360
112	113	0.0019017	0.0006843	0	100	100	100	0	0	1	-360	360
100	114	0.0050430	0.0018149	0	100	100	100	0	0	1	-360	360
114	115	0.0015421	0.0010496	0	100	100	100	0	0	1	-360	360
115	116	0.0030843	0.0020331	0	100	100	100	0	0	1	-360	360
116	117	0.0033471	0.0030331	0	100	100	100	0	0	1	-360	360
117	118	0.0040413	0.0036198	0	100	100	100	0	0	1	-360	360

46	27	0.0043455	0.0024174	0	100	100	100	0	0	1	-360	360
17	27	0.0043455	0.0024099	0	100	100	100	0	0	1	-360	360
8	24  0.0035306	0.0012719	0	100	100	100	0	0	1	-360	360
54	43	0.0039669	0.0014281	0	100	100	100	0	0	1	-360	360
62	49	0.0029752	0.0010711	0	100	100	100	0	0	1	-360	360
37	62	0.0047107	0.0047273	0	100	100	100	0	0	1	-360	360
9	40	0.0043802	0.0027669	0	100	100	100	0	0	1	-360	360
58	96	0.0032702	0.0011777	0	100	100	100	0	0	1	-360	360
73	91	0.0056198	0.0053554	0	100	100	100	0	0	1	-360	360
88	75	0.0033570	0.0012099	0	100	100	100	0	0	1	-360	360
99	77	0.0038231	0.0013835	0	100	100	100	0	0	1	-360	360
108	83	0.0053802	0.0019339	0	100	100	100	0	0	1	-360	360
105	86	0.0067149	0.0024174	0	100	100	100	0	0	1	-360	360
110	118	0.0058587	0.0021099	0	100	100	100	0	0	1	-360	360
25	35	0.0041322	0.0041322	0	100	100	100	0	0	1	-360	360
];
code = [
27  26  25  24  23  22  21  20  19  18  11  10  4   28  29  38  39  40  41  42  43  44  45  46  119
17  16  15  14  13  12  18  19  20  21  22  23  24  25  26  27  120 0   0   0   0   0   0   0   0
24  23  22  21  20  19  18  11  10  4   5   6   7   8   121 0   0   0   0   0   0   0   0   0   0   
43  42  41  40  39  38  30  31  32  33  34  35  47  48  49  50  51  52  53  54  122 0   0   0   0
49  48  47  35  34  33  32  31  30  55  56  57  58  59  60  61  62  123 0   0   0   0   0   0   0

37  36  30  55  56  57  58  59  60  61  62  124 0   0   0   0   0   0   0   0   0   0   0   0   0
9   8   7   6   5   28  29  38  39  40  125 0   0   0   0   0   0   0   0   0   0   0   0   0   0
58  57  56  55  29  28  4   2   63  64  65  89  90  91  96  126 0   0   0   0   0   0   0   0   0
91  90  89  66  67  68  69  70  71  72  73  127 0   0   0   0   0   0   0   0   0   0   0   0   0
75  74  73  72  71  70  69  68  67  66  65  78  79  86  87  88  128 0   0   0   0   0   0   0   0

99  98  97  96  91  90  89  66  67  68  69  70  71  72  73  74  75  76  77  129 0   0   0   0   0
83  82  81  80  79  78  64  63  100 101 102 103 104 105 106 107 108 130 0   0   0   0   0   0   0
86  79  78  64  63  100 101 102 103 104 105 131 0   0   0   0   0   0   0   0   0   0   0   0   0
110 109 108 107 106 105 104 103 102 101 114 115 116 117 118 132 0   0   0   0   0   0   0   0   0
25  24  23  22  21  20  19  18  11  10  4   28  29  30  31  32  33  34  35  133 0   0   0   0   0
];
delete_branch = zeros(15,1);
for i = 1:15
    delete_branch(i,1) = code(i,chorm(i));
end

branch_data(delete_branch-1,:)=[];


% no_delete_same_point = 0;
% [m_branch_first_test,n_branch_first_test] = size(branch_data);
% if m_branch_first_test > 117
%     no_delete_same_point = 1;
%     flag = 0;
% end
% 
% if no_delete_same_point == 0




[m_branch_data, n_branch_data] = size(branch_data);
if m_branch_data >117
    flag = 0;
else 
    further_test_2 = 1;
end

if further_test_2 ==1
node_searched       =   zeros(118,2);
for i = 1:118
    node_searched(i,1)  =  i;
end
node_searched(1,2)  =   1;


% 	Find the original source, node No. 1
num_row_1	 		= find(branch_data(:,1)==1);
[m_1,n_1] 			= size(num_row_1);
% node_next
% The next nodes information of the current line segment, this number is a matrix
node_next			= ones(m_1,n_1);
for i = 1:m_1
	node_next(i,1) 	= branch_data(num_row_1(i,1), 2);
    node_searched(node_next(i,1),2) =  1;
end

% Delete the processed data(The line segment of first node is No. 1 )
branch_data(num_row_1,:) = [];
% Size the matrix of the branch 
[m_branch,n_branch] = size(branch_data);




clear m_1;
clear n_1;
flag =1;
 
% Loop when all the data is not complete
% When find a loop or a isolate island, set m_branch = 0 and flag = 0;
while (m_branch ~=0)
		current_node 		= 	node_next;
        clear node_next;
		[m_c, n_v]			=	size(current_node);
        if m_c == 0
            m_branch = 0;
            break;
        end
		%	Find in the first line
		num_row_next 		=	find(branch_data(:,1)==current_node(1,1)); 
		for i = 2:m_c
			num_row_next 	=  [num_row_next;find(branch_data(:,1)==current_node(i,1))];
        end
		
		[m_n1,n_n1]			=	size(num_row_next);
        node_next           =   ones(m_n1,n_n1);
		for i =1:m_n1
			node_next(i,1)	=	branch_data(num_row_next(i,1),2);
            node_searched(node_next(i,1),2) =  1;
		end
		
		
		
		% Find in the second line
		for i = 1:m_c
			num_row_next 	=  [num_row_next;find(branch_data(:,2)==current_node(i,1))];
        end
		
		[m_n2,n_n2]			=	size(num_row_next);
        
		for i =(1+m_n1):m_n2
			node_next(i,1)	=	branch_data(num_row_next(i,1),1);
            node_searched(node_next(i,1),2) =  1;
        end
        % Delete all the current information
        branch_data(num_row_next,:) = [];
        clear current_node;
        clear num_row_next;
        % Calculate the size of the branch 
        [m_branch,n_branch] = size(branch_data);
             
end


flag = all(node_searched(:,2)==1);


end
end
end






